* Firm organization with multiple establishments
* Section V: Appendix Tables D.17 (establishment level layer definition)
* Middle panel: Headquarters

clear all 
set matsize 2000
set more off

capture log close
log using log/24b_train_layer-est_hq.log, replace

use data/train_analysis_est.dta, clear

********************************************************************************
*** Table D.17, full sample, middle left

{
preserve
keep if hauptbet == 1 & e_traindata == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.17, middle left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.17, firms with at least two establishments, middle left

{
preserve
keep if hauptbet == 1 & e_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl hq_kreis jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr hqcounty_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'
sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.17, middle right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

log close
